//
//  DDroppingViewManager.swift
//  Drop
//
//  Created by hushaohua on 2017/12/22.
//  Copyright © 2017年 init. All rights reserved.
//

import UIKit
import CoreGraphics

class DDroppingViewManager: NSObject {
    
    override init() {
        super.init();
       
    }
    
    //MARK: - dropping
    func droppingFrame() -> CGRect {
        let size = UIScreen.main.bounds;
        return CGRect(x:size.width / 3,y:size.height - 74,width:size.width / 3,height:64);
    }
    
    lazy var gradientBackground:UIImageView = {
        var frame = self.droppingFrame();
        frame.origin.x = -10;
        frame.size.width += 20;
        frame.origin.y = (frame.size.height - frame.size.width) / 2 + 20;
        frame.size.height = frame.size.width;
        var imageView = UIImageView(frame: frame);
        
        UIGraphicsBeginImageContext(frame.size);
        guard let context = UIGraphicsGetCurrentContext() else{
            return imageView;
        }
        let colorSpace = CGColorSpaceCreateDeviceRGB();
        let colors:[CGColor] = [UIColor.h_darkBlue().cgColor, UIColor(white:1,alpha:0).cgColor];
        let locations:[CGFloat] = [0, 1];
        let layer:CAGradientLayer = CAGradientLayer();
//        let path:UIBezierPath = UIBezierPath(ovalIn: frame);
//        context.addPath(path.cgPath);
        let gradient = CGGradient(colorsSpace: colorSpace, colors: colors as CFArray, locations: locations);
        let center = CGPoint(x:frame.size.width / 2, y:frame.size.height / 2);
        let endRadius = frame.size.height / 2;
        let startRadius:CGFloat = 30;
        
        context.drawRadialGradient(gradient!,
                                   startCenter: center, startRadius: startRadius,
                                   endCenter: center, endRadius: endRadius,
                                   options: .drawsBeforeStartLocation)
        let image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        imageView.image = image;
        return imageView;
    }();
    
    lazy var tabWindow:UIWindow = {
        var window:UIWindow = UIWindow(frame: self.droppingFrame());
//        window.backgroundColor = UIColor.yellow;
//        window.clipsToBounds = true;
        return window;
    }();
    
    func setupDroppingWindow(){
        self.droppingPresenter.view = self.droppingView;
        self.tabWindow.addSubview(self.gradientBackground);
        self.gradientBackground.isHidden = true;
        self.tabWindow.addSubview(self.droppingView);
        self.tabWindow.isHidden = false;
    }
    
    lazy var droppingView:DDroppingView = {
        var frame = self.droppingFrame();
        frame.origin = .zero;
        var view:DDroppingView = DDroppingView(frame: frame);
        view.clipsToBounds = true;
        view.dropSize = CGSize(width:64, height:58);
        view.dropView.dripSize = CGSize(width:64, height:32);
        view.intervalSize = CGSize(width:frame.size.width, height:15);
        view.layout();
        view.backgroundColor = UIColor.clear;
        view.isUserInteractionEnabled = true;
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapDropping(_:)));
        view.addGestureRecognizer(tap);
        return view;
    }();
    lazy var droppingPresenter:DDroppingViewPresenter = {
        var presenter:DDroppingViewPresenter = DDroppingViewPresenter();
        return presenter;
    }();
    
    lazy var dropIntoViewController:DDropIntoViewController = {
        var controller:DDropIntoViewController = DDropIntoViewController(nibName: nil, bundle: nil);
        controller.modalTransitionStyle = .crossDissolve;
        controller.intoViewPreseter.backClosure = {
            self.dismissInto();
        };
        controller.backClosure = {
            self.dismissInto();
        };
        return controller;
    }();
    
    func dismissInto() -> Void{
        let controller = UIApplication.shared.keyWindow?.rootViewController;
        self.droppingView.setColor(UIColor.black);
        self.gradientBackground.isHidden = true;
        controller?.dismiss(animated: true, completion: {
            self.dropIntoViewController.didShow();
            self.dropIntoViewController.intoViewPreseter.updatingDrop = nil;
        });
    }
    
    func gotoDropInto(_ drop:Drop){
        let controller = UIApplication.shared.keyWindow?.rootViewController;
        if (self.dropIntoViewController.presentingViewController == nil){
            self.droppingView.setColor(UIColor.white);
            self.gradientBackground.isHidden = false;
            self.dropIntoViewController.intoViewPreseter.updatingDrop = drop;
            controller?.present(self.dropIntoViewController, animated: true) {
                
            };
        }
    }
    
    @objc func tapDropping(_ sender:UIGestureRecognizer) -> Void{
        let controller = UIApplication.shared.keyWindow?.rootViewController;
        if (self.dropIntoViewController.presentingViewController != nil){
            self.droppingView.setColor(UIColor.black);
            self.gradientBackground.isHidden = true;
            self.dropIntoViewController.intoViewPreseter.updatingDrop = nil;
            controller?.dismiss(animated: true, completion: {
                self.dropIntoViewController.didShow();
            });
        }else{
            self.droppingView.setColor(UIColor.white);
            self.gradientBackground.isHidden = false;
            controller?.present(self.dropIntoViewController, animated: true) {
                
            };
        }
    }
    
    func startAtDate(_ date:Date?) -> Void{
        if (date != nil){
            self.droppingPresenter.beginDate = date;
            self.droppingPresenter.startDropping();
        }
    }
    
    func stop() -> Void{
        self.droppingPresenter.stopDropping();
    }
    
}
